/*
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

package com.mirth.connect.client.ui.panels.connectors;

import com.mirth.connect.client.ui.UIConstants;
import com.mirth.connect.donkey.model.channel.ListenerConnectorProperties;
import com.mirth.connect.donkey.model.channel.ListenerConnectorPropertiesInterface;

public class ListenerSettingsPanel extends javax.swing.JPanel {

    public static final String FIELD_PORT = ListenerSettingsPanel.class.getSimpleName() + ".PORT";

    public ListenerSettingsPanel() {
        initComponents();
    }

    public void setProperties(ListenerConnectorPropertiesInterface propertiesInterface) {
        ListenerConnectorProperties properties = propertiesInterface.getListenerConnectorProperties();

        addressField.setText(properties.getHost());
        updateAddressRadio();

        portField.setText(properties.getPort());
    }

    public void fillProperties(ListenerConnectorPropertiesInterface propertiesInterface) {
        ListenerConnectorProperties properties = propertiesInterface.getListenerConnectorProperties();

        properties.setHost(addressField.getText());
        properties.setPort(portField.getText());
    }

    public boolean checkProperties(ListenerConnectorPropertiesInterface propertiesInterface, boolean highlight) {
        ListenerConnectorProperties properties = propertiesInterface.getListenerConnectorProperties();

        boolean valid = true;

        if (properties.getHost().length() == 0) {
            valid = false;
            if (highlight) {
                addressField.setBackground(UIConstants.INVALID_COLOR);
            }
        }

        if (properties.getPort().length() == 0) {
            valid = false;
            if (highlight) {
                portField.setBackground(UIConstants.INVALID_COLOR);
            }
        }

        return valid;
    }

    public void resetInvalidProperties() {
        addressField.setBackground(null);
        portField.setBackground(null);
    }

    private void updateAddressRadio() {
        if (addressField.getText().equals(new ListenerConnectorProperties(null).getHost())) {
            allRadio.setSelected(true);
            allRadioActionPerformed(null);
        } else {
            specificRadio.setSelected(true);
            specificRadioActionPerformed(null);
        }
    }

    // @formatter:off
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        listenerButtonGroup = new javax.swing.ButtonGroup();
        addressLabel = new javax.swing.JLabel();
        allRadio = new com.mirth.connect.client.ui.components.MirthRadioButton();
        specificRadio = new com.mirth.connect.client.ui.components.MirthRadioButton();
        addressField = new com.mirth.connect.client.ui.components.MirthTextField();
        portLabel = new javax.swing.JLabel();
        portField = new com.mirth.connect.client.ui.components.MirthTextField();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createMatteBorder(1, 0, 0, 0, new java.awt.Color(204, 204, 204)), "Listener Settings", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        addressLabel.setText("Local Address:");

        allRadio.setBackground(new java.awt.Color(255, 255, 255));
        allRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        listenerButtonGroup.add(allRadio);
        allRadio.setText("All interfaces");
        allRadio.setToolTipText("<html>If checked, the connector will listen on all interfaces, using address 0.0.0.0.</html>");
        allRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        allRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allRadioActionPerformed(evt);
            }
        });

        specificRadio.setBackground(new java.awt.Color(255, 255, 255));
        specificRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        listenerButtonGroup.add(specificRadio);
        specificRadio.setText("Specific interface:");
        specificRadio.setToolTipText("<html>If checked, the connector will listen on the specific interface address defined.</html>");
        specificRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        specificRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                specificRadioActionPerformed(evt);
            }
        });

        addressField.setToolTipText("The DNS domain name or IP address on which the web service should listen for connections.");

        portLabel.setText("Local Port:");

        portField.setToolTipText("The port on which the web service should listen for connections.");
        portField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                portFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(portLabel)
                    .addComponent(addressLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(allRadio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(specificRadio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addressField, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addressLabel)
                    .addComponent(allRadio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(specificRadio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portLabel)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        getAccessibleContext().setAccessibleName("Listener Settings");
    }// </editor-fold>//GEN-END:initComponents
    // @formatter:on

    private void allRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allRadioActionPerformed
        addressField.setText(new ListenerConnectorProperties(null).getHost());
        addressField.setEnabled(false);
    }//GEN-LAST:event_allRadioActionPerformed

    private void specificRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_specificRadioActionPerformed
        addressField.setEnabled(true);
    }//GEN-LAST:event_specificRadioActionPerformed

    private void portFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_portFieldKeyReleased
        ((ConnectorPanel) getParent()).updatedField(FIELD_PORT);
    }//GEN-LAST:event_portFieldKeyReleased
     // Variables declaration - do not modify//GEN-BEGIN:variables

    private com.mirth.connect.client.ui.components.MirthTextField addressField;
    private javax.swing.JLabel addressLabel;
    private com.mirth.connect.client.ui.components.MirthRadioButton allRadio;
    private javax.swing.ButtonGroup listenerButtonGroup;
    private com.mirth.connect.client.ui.components.MirthTextField portField;
    private javax.swing.JLabel portLabel;
    private com.mirth.connect.client.ui.components.MirthRadioButton specificRadio;
    // End of variables declaration//GEN-END:variables
}
